from mmap import MMap
from word import Word

class MLib:

    def __init__(self, maps=[], words=[]):
        self.words = words
        self.maps = maps

    def get_words(self):
        return self.words

    def add_word(self, name):
        w = Word(name, None)
        self.words.append(w)
        return w

    def get_word(self, name, parent=None):
        fws = filter(lambda w: (w.get_name() == name), self.words)
        if len(fws)>0:
            return fws[0]
        else:
            w = Word(name, parent)
            self.words.append(w)
            return w

    def remove_uniq_word(self, word):
        mms = filter(lambda m: word in m.get_words(), self.maps)
        if len(mms) == 0:
            self.words.remove(word)
        return self.words
    
    def get_similar_words(self):
        pass

    def mmap_new(self, name):
        mmap = MMap(self, name)        
        return mmap

    def mmap_remove(self, mmap):
        self.maps.remove(mmap)
        return self.maps

    def update_maps(self, mmap):
        self.maps.append(mmap)
        return self.maps

    # Вернёт карту по имени, а если совпадения по имени нет - создаст новую
    def get_map_by_name(self, name):
        mms = filter(lambda m: m.get_name() == name, self.maps)
        mms[0] if count(mms)==1 else self.mmap_new(name)

    # Вернуть список карт
    def get_mmaps(self):
        return self.maps
